local Class = require('Core.Class')
local BasePanel = require('Game.UI.Base.BasePanel')
local EventManager = require('Game.Manager.EventManager')
local UIManager = require('Game.Manager.UIManager')

local StartPanel = Class('StartPanel', BasePanel)

function StartPanel:__init()
    self.onePlayerBtn = self.transform:Find('OnePlayerButton'):GetComponent('Button')
    self.twoPlayerBtn = self.transform:Find('TwoPlayerButton'):GetComponent('Button')

    self.onPlayEvent = nil
end

function StartPanel:OnShow()
    BasePanel.OnShow(self)

    -- 添加开始游戏事件
    self:AddPlayGameEvent()

     --  发出事件 单人模式开始游戏
    self.onePlayerBtn.onClick:AddListener(function()
        EventManager:Fire('OnPlayGame', 1)
    end)

    --  发出事件 双人模式开始游戏
    self.twoPlayerBtn.onClick:AddListener(function()
        EventManager:Fire('OnPlayGame', 2)
    end)
end

function StartPanel:OnHide()
    BasePanel.OnHide(self)

    -- 移除EventMgr的事件
    EventManager:RemoveListener('OnPlayGame', self.onPlayEvent)

    --  移除按钮事件
    self.onePlayerBtn.onClick:RemoveAllListeners()
    self.twoPlayerBtn.onClick:RemoveAllListeners()
end

function StartPanel:AddPlayGameEvent()
    self.onPlayEvent = function ()
        UIManager:HidePanel(self.panelName)
    end
    EventManager:AddListener('OnPlayGame', self.onPlayEvent)
end

return StartPanel